﻿using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.Domain.wdx.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.ResidentCommand;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.ResidentCommandHandler
{
    public class GetResidentCommandHandler : IRequestHandler<GetResidentCommand, APIResult<View_GetResident>>
    {
        private readonly SqlSugarContext sqlSugar;

        public GetResidentCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }

        public Task<APIResult<View_GetResident>> Handle(GetResidentCommand request, CancellationToken cancellationToken)
        {
            APIResult<View_GetResident> result = new APIResult<View_GetResident>();
            var model = sqlSugar.db.Queryable<ResidentFiles>().First(x => x.ResidentFiles_Name == request.ResidentFiles_Name);
            View_GetResident obj = new View_GetResident()
            {
                ResidentFiles_Id = model.ResidentFiles_Id,
                ResidentFiles_Name = model.ResidentFiles_Name,
                IdCard = model.IdCard,
                Sex = model.Sex == false ? "女" : "男",
                Age = model.Age,
                Birthday = model.Birthday.DateTimeToString("yyyy-MM-dd HH:ff:ss", "--"),
            };
            if (model == null) { result.Code = APIEnum.查询失败;result.Msg = APIEnum.查询失败.ToString(); }
            else { result.data = obj;result.Code = APIEnum.查询成功;result.Msg = APIEnum.查询成功.ToString(); }
            return Task.FromResult(result);
        }
    }
}
